package cnsatmsmart.automation.mmi.view.layers;

import com.bbn.openmap.LatLonPoint;
import java.awt.Color;
import java.util.Iterator;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import cnsatmsmart.automation.mmi.controller.Buffer.Flight;

/**
 * @author SIK - PTIK, BPPT
 **/

public class InsetLayer {
    /* atribut - atribut yang dimiliki oleh kelas InsetLayer */
        private List flightList;
        private LatLonPoint topleft;
        private LatLonPoint bottomright;

    /* constructor - user defined ctor, no default ctor */
        // <editor-fold defaultstate="collapsed" desc="ctor">
        /**
         * menginstansiasi object InsetLayer<br />
         * #do nothing ctor
         */
        public InsetLayer() { }
        // </editor-fold>

    /* method - method yang dimiliki oleh kelas InsetLayer */
        /**
         * menggambarkan diagram
         * @param flightList List yang merupakan senarai dari pesawat yang akan diplot
         * @param topleft LatLonPoint yang merupakan posisi kiri atas dari koordinat yang akan diplot
         * @param bottomright LatLonPoint yang merupakan posisi kanan bawah dari koordinat yang akan diplot
         * @return ChartPanel yang merupakan container dari graphic yang diplot
         */
        public ChartPanel drawChart(List flightList, LatLonPoint topleft, LatLonPoint bottomright) {   //menggambarkan diagram
            this.flightList = flightList;
            this.topleft = topleft;
            this.bottomright = bottomright;

            final XYDataset dataset = createDataSet();
            final JFreeChart chart = createChart(dataset);
            final ChartPanel chartPanel = new ChartPanel(chart);

            return (chartPanel);
        }

        private XYDataset createDataSet () {                                    //membangkitkan dataset
            final XYSeries flights = new XYSeries("Flight");

            float min = ((this.flightList == null) || (this.flightList.isEmpty())) ? 0 : ((Flight)flightList.get(0)).getAltitude(), max = ((this.flightList == null) || (this.flightList.isEmpty())) ? 0 : ((Flight)flightList.get(0)).getAltitude();

            Iterator flightsIter = this.flightList.iterator();
            while (flightsIter.hasNext()) {
                Flight flight = (Flight) flightsIter.next();
                if (((flight.getLatitude() <= this.topleft.getLatitude()) && (flight.getLatitude() >= this.bottomright.getLatitude())) && ((flight.getLongitude() >= this.topleft.getLongitude()) && (flight.getLongitude() <= this.bottomright.getLongitude()))) {
                    flights.add(flight.getLongitude(), flight.getAltitude());
                    if (min > flight.getAltitude()) {
                        min = flight.getAltitude();
                    }
                    if (max < flight.getAltitude()) {
                        max = flight.getAltitude();
                    }
                }
            }

            final XYSeriesCollection RetVal = new XYSeriesCollection();
            RetVal.addSeries(flights);

            final XYSeries bounds = new XYSeries("Bounds");

            bounds.add(this.topleft.getLongitude(), max);
            bounds.add(this.bottomright.getLongitude(), max);

            RetVal.addSeries(bounds);

            return (RetVal);
        }
        private JFreeChart createChart (final XYDataset dataset) {              //membangkitkan diagram
            final JFreeChart chart = ChartFactory.createXYLineChart(
                    null,           //Chart Title
                    "Longitude (°)",//Absis Axis Label
                    "Altitude (fl)",//Ordinat Axis Label
                    dataset,        //Data
                    PlotOrientation.VERTICAL,
                    false,          //includes legends
                    true,           //includes tooltips
                    false           //includes URLs
                    );

            chart.setBackgroundPaint(Color.WHITE);

            final XYPlot plot = chart.getXYPlot();
            plot.setBackgroundPaint(Color.LIGHT_GRAY);
            plot.setDomainGridlinePaint(Color.WHITE);
            plot.setRangeGridlinePaint(Color.WHITE);

            final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
            renderer.setSeriesLinesVisible(0, false);
            renderer.setSeriesShapesVisible(1, false);
            plot.setRenderer(renderer);

            final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
            rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

            Iterator flightsIter = this.flightList.iterator();
            while (flightsIter.hasNext()) {
                Flight flight = (Flight) flightsIter.next();
                if (((flight.getLatitude() <= this.topleft.getLatitude()) && (flight.getLatitude() >= this.bottomright.getLatitude())) && ((flight.getLongitude() >= this.topleft.getLongitude()) && (flight.getLongitude() <= this.bottomright.getLongitude()))) {
                    XYTextAnnotation annotation = new XYTextAnnotation(flight.getCallSign(), flight.getLongitude(), flight.getAltitude());
                    plot.addAnnotation(annotation);
                }
            }

            return (chart);
        }
}
